package org.csy.work.dao.impl;



import org.csy.work.dao.ProductTypeDao;
import org.csy.work.entity.ProductType;
import org.csy.work.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZJL
 * @date 2024-11-27
 */
public class ProductTypeDaoImpl implements ProductTypeDao {

    @Override
    public List<ProductType> getProductTypeAll() {
        List<ProductType> list = new ArrayList<ProductType>();
        String sql = "select type_id,type_name,parent_id" +
                " from product_type";
        Connection con = DbUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                ProductType productType = new ProductType();
                productType.setTypeId(rs.getInt("type_id"));
                productType.setTypeName(rs.getString("type_name"));
                productType.setParentId(rs.getInt("parent_id"));
                list.add(productType);
            }
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(rs, ps, con);
        }
        return list;
    }
}
